﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfMail.Interface;

namespace WcfMail
{
    /// <summary>
    /// “工人”抽象类
    /// </summary>
    public abstract  class Workingman
    {
        public Workingman()
        {
            Prepare();
        }
        /// <summary>
        /// 当前工作的名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 工作消息
        /// </summary>
        public string WorkMesssage { get; protected set; }
        /// <summary>
        /// 本次操作的错误信息
        /// </summary>
        public string ErrorMessage { get; protected set; }
        /// <summary>
        /// 做工作（具体工作任务由子类实现）
        /// </summary>
        public abstract void  DoWork();
       /// <summary>
        /// 发送反馈消息，需要在子类中实现
       /// </summary>
        /// <param name="message">反馈信息的正文</param>
       /// <returns>反馈操作是否成功</returns>
        public virtual  bool SendFeedback(FeedbackMessage message)
        {
            return true;
        }
        /// <summary>
        /// 初始化准备，如从配置文件初始化配置，需要子类具体实现。该方法会被构造函数调用。
        /// </summary>
        protected  virtual void Prepare()
        {
            BeginWork();
            work_on = true;
        }
        /// <summary>
        /// 工作中事件
        /// </summary>
        public event EventHandler Working;
        /// <summary>
        /// 错误发生事件
        /// </summary>
        public event EventHandler OnError;


        public void BeginWork()
        {
            this.WorkMesssage = "";
            this.ErrorMessage = "";
        }
        /// <summary>
        /// 工作是否继续
        /// </summary>
        protected bool work_on = false;

        public void StopWork()
        {
            work_on = false;
        }
        /// <summary>
        /// 告知一条消息
        /// </summary>
        /// <param name="message"></param>
        public void Talk(string message)
        {
            this.WorkMesssage = message;
            if (this.Working != null)
                Working(this, new EventArgs());
        }
        /// <summary>
        /// 警告消息
        /// </summary>
        /// <param name="errorMessage"></param>
        public void Alert(string errorMessage)
        {
            this.ErrorMessage = errorMessage;
            if (this.OnError != null)
                OnError(this, new EventArgs());
        }
        /// <summary>
        /// 反馈
        /// </summary>
        /// <returns></returns>
        public virtual bool Feedback() { return true; }

        protected  delegate bool WorkMethod();
        protected delegate void WorkAfterMethod();

        protected void DoWork(WorkMethod method, WorkAfterMethod afterMethodTrue, WorkAfterMethod afterMethodFlase)
        {
            if (method())
            {
                afterMethodTrue();
                this.Talk("本次工作结束。");
            }
            else
            {
                afterMethodFlase();
                this.Talk("本次工作结束,但发成了错误，请检查错误信息");
            }
        }
    }

    }
